home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / bib-mode.el.z / bib-mode.el
Lisp/Scheme  |  1998-10-27  |  7KB  |  243 lines

  1. ;;; bib-mode.el --- bib-mode, major mode for editing bib files.
  2.  
  3. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: bib
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;;   GNU Emacs code to help maintain databases compatible with (troff)
  28. ;;   refer and lookbib.  The file bib-file should be set to your 
  29. ;;   bibliography file.  Keys are automagically inserted as you type,
  30. ;;   and appropriate keys are presented for various kinds of entries.
  31.  
  32. ;;; Code:
  33.  
  34. (defvar bib-file "~/my-bibliography.bib" 
  35.    "Default name of file used by `addbib'.")
  36.  
  37. (defvar unread-bib-file "~/to-be-read.bib"
  38.    "Default name of file used by `unread-bib' in Bib mode.")
  39.  
  40. (defvar bib-mode-map (copy-keymap text-mode-map))
  41. (define-key bib-mode-map "\C-M" 'return-key-bib)
  42. (define-key bib-mode-map "\C-c\C-u" 'unread-bib)
  43. (define-key bib-mode-map "\C-c\C-@" 'mark-bib)
  44. (define-key bib-mode-map "\e`" 'abbrev-mode)
  45. (defvar bib-mode-abbrev-table nil
  46.    "Abbrev table used in Bib mode")
  47.  
  48. (defun addbib ()
  49.    "Set up editor to add to troff bibliography file specified 
  50. by global variable `bib-file'.  See description of `bib-mode'."
  51.    (interactive)
  52.    (find-file bib-file)
  53.    (goto-char (point-max))
  54.    (bib-mode)
  55.    )
  56.    
  57. (defun bib-mode ()
  58.    "Mode for editing `lookbib' style bibliographies.  
  59. Hit RETURN to get next % field key.
  60. If you want to ignore this field, just hit RETURN again.
  61. Use `text-mode' to turn this feature off.
  62.  
  63.  journal papers:                    A* T D J V N P K W X
  64.  articles in books & proceedings:   A* T D B E* I C P K W X 
  65.  tech reports:                      A* T D R I C K W X
  66.  books:                             A* T D I C K W X
  67.  
  68. Fields:
  69.  
  70. A uthor        T itle        D ate          J ournal
  71. V olume        N umber        P age        K eywords
  72. B in book or proceedings    E ditor        C ity & state
  73. I nstitution, school, or publisher
  74. R eport number or 'phd thesis' or 'masters thesis' or 'draft' or 
  75.      'unnumbered' or 'unpublished'
  76. W here can be found locally (login name, or ailib, etc.)
  77. X comments (not used in indexing)
  78.  
  79. \\[unread-bib] appends current entry to a different file (for example,
  80. a file of papers to be read in the future), given by the value of the
  81. variable `unread-bib-file'.
  82. \\[mark-bib] marks current or previous entry.
  83. Abbreviations are saved in `bib-mode-abbrev-table'.
  84. Hook can be stored in `bib-mode-hook'.
  85. Field keys given by variable `bib-assoc'.
  86.  
  87. Commands:
  88. \\{bib-mode-map}
  89. "
  90.    (interactive)
  91.    (text-mode)
  92.    (use-local-map bib-mode-map)
  93.    (setq mode-name "Bib")
  94.    (setq major-mode 'bib-mode)
  95.    (define-abbrev-table 'bib-mode-abbrev-table ())
  96.    (setq local-abbrev-table bib-mode-abbrev-table)
  97.    (abbrev-mode 1)
  98.    (run-hooks 'bib-mode-hook)
  99.    )
  100.  
  101. (defconst bib-assoc '(
  102.            (" *$" . "%A ")
  103.            ("%A ." . "%A ")
  104.            ("%A $" . "%T ")
  105.            ("%T " . "%D ")
  106.            ("%D " . "%J ")
  107.            ("%J ." . "%V ")
  108.            ("%V " . "%N ")
  109.            ("%N " . "%P ")
  110.            ("%P " . "%K ")
  111.            ("%K " . "%W ")
  112.            ("%W " . "%X ")
  113.            ("%X " . "")
  114.            ("%J $" . "%B ")
  115.            ("%B ." . "%E ")
  116.            ("%E ." . "%E ")
  117.            ("%E $" . "%I ")
  118.            ("%I " . "%C ")
  119.            ("%C " . "%P ")
  120.            ("%B $" . "%R ")
  121.            ("%R " . "%I ")
  122.            )
  123.            
  124. "Describes bibliographic database format.  A line beginning with
  125. the car of an entry is followed by one beginning with the cdr.
  126. ")
  127.  
  128. (defun bib-find-key (slots)
  129.    (cond
  130.       ((null slots)
  131.      (if (bobp)
  132.         ""
  133.         (progn (previous-line 1) (bib-find-key bib-assoc))))
  134.       ((looking-at (car (car slots)))
  135.      (cdr (car slots)))
  136.       (t (bib-find-key (cdr slots)))
  137.       ))
  138.  
  139.  
  140. (defvar bib-auto-capitalize t 
  141. "*True to automatically capitalize appropriate fields in Bib mode.")
  142.  
  143. (defconst bib-capitalized-fields "%[AETCBIJR]")
  144.  
  145. (defun return-key-bib ()
  146.   "Magic when user hits return, used by `bib-mode'."
  147.   (interactive)
  148.   (if (eolp)
  149.     (let (empty new-key beg-current end-current)
  150.       (beginning-of-line)
  151.       (setq empty (looking-at "%. $"))
  152.       (if (not empty)
  153.     (progn
  154.       (end-of-line)
  155.       (newline)
  156.       (forward-line -1)
  157.       ))
  158.       (end-of-line)
  159.       (setq end-current (point))
  160.       (beginning-of-line)
  161.       (setq beg-current (point))
  162.       (setq new-key (bib-find-key bib-assoc))
  163.       (if (and (not empty) bib-auto-capitalize
  164.         (looking-at bib-capitalized-fields))
  165.     (save-excursion
  166.       (capitalize-title-region (+ (point) 3) end-current)))
  167.       (goto-char beg-current)
  168.       (if empty
  169.     (kill-line nil)
  170.     (forward-line 1)
  171.     )
  172.       (insert-string new-key))
  173.     (newline)))
  174.  
  175. (defun mark-bib ()
  176.    "Set mark at beginning of current or previous bib entry, point at end."
  177.    (interactive)
  178.    (beginning-of-line nil)
  179.    (if (looking-at "^ *$") (re-search-backward "[^ \n]" nil 2))
  180.    (re-search-backward "^ *$" nil 2)
  181.    (re-search-forward "^%")
  182.    (beginning-of-line nil)
  183.    (push-mark (point))
  184.    (re-search-forward "^ *$" nil 2)
  185.    (next-line 1)
  186.    (beginning-of-line nil))
  187.  
  188. (defun unread-bib ()
  189.    "Append current or previous entry to file of unread papers
  190. named by variable `unread-bib-file'."
  191.    (interactive)
  192.    (mark-bib)
  193.    (if (get-file-buffer unread-bib-file)
  194.       (append-to-buffer (get-file-buffer unread-bib-file) (mark) (point))
  195.       (append-to-file (mark) (point) unread-bib-file)))
  196.  
  197.  
  198. (defvar capitalize-title-stop-words
  199.    (concat
  200.       "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
  201.       "by\\|with\\|that\\|its")
  202.    "Words not to be capitalized in a title (unless they're the first word
  203. in the title).")
  204.  
  205. (defvar capitalize-title-stop-regexp
  206.    (concat "\\(" capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
  207.  
  208. (defun capitalize-title-region (begin end)
  209.    "Like `capitalize-region', but don't capitalize stop words, except the first."
  210.    (interactive "r")
  211.    (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
  212.       (unwind-protect
  213.      (save-restriction
  214.         (set-syntax-table text-mode-syntax-table)
  215.         (narrow-to-region begin end)
  216.         (goto-char (point-min))
  217.         (if (looking-at "[A-Z][a-z]*[A-Z]")
  218.            (forward-word 1)
  219.            (capitalize-word 1))
  220.         (while (re-search-forward "\\<" nil t)
  221.            (if (looking-at "[A-Z][a-z]*[A-Z]")
  222.           (forward-word 1)
  223.           (if (let ((case-fold-search t))
  224.              (looking-at capitalize-title-stop-regexp))
  225.              (downcase-word 1)
  226.              (capitalize-word 1)))
  227.            ))
  228.      (set-syntax-table orig-syntax-table))))
  229.  
  230.  
  231. (defun capitalize-title (s)
  232.    "Like `capitalize', but don't capitalize stop words, except the first."
  233.    (save-excursion
  234.       (set-buffer (get-buffer-create "$$$Scratch$$$"))
  235.       (erase-buffer)
  236.       (insert s)
  237.       (capitalize-title-region (point-min) (point-max))
  238.       (buffer-string)))
  239.  
  240. (provide 'bib-mode)
  241.  
  242. ;;; bib-mode.el ends here
  243.